import 'dart:math';

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lighting_driver/src/bluetooth/send_data.dart';
import 'package:lighting_driver/src/const/const.dart';
import 'package:lighting_driver/src/moduls/home/model/model.dart';

import '../../../../generated/my_localizations.dart';
import '../../../service/navigation_service.dart';

class HomeController extends GetxController {
  RxString road = ''.obs;
  RxString weather = ''.obs;
  RxString mode = ''.obs;
  late final List<List<Model>> data;
  Rx<Model> selectModel = Model(yellow: 50, light: 50).obs;
  DateTime _lastSendTime = DateTime.fromMillisecondsSinceEpoch(0); // 上次发送时间
  final Duration _sendInterval = const Duration(milliseconds: 200); // 最小间隔时间
  MyLocalizations? loc;

  @override
  void onInit() {
    super.onInit();
    List<String> name1 = [assets.icon1, assets.icon2, assets.icon3, assets.icon4];
    List<String> name2 = [assets.icon5, assets.icon6, assets.icon7, assets.icon8];
    List<String> name3 = ['100', '75', '50', assets.icon9];
    List<String> name4 = ['100', '75', '50', assets.icon10];
    List<String> name5 = ['100', '75', '50', assets.icon11];

    List<bool> isPic1 = [true, true, true, true];
    List<bool> isPic2 = [true, true, true, true];
    List<bool> isPic3 = [false, false, false, true];
    List<bool> isPic4 = [false, false, false, true];
    List<bool> isPic5 = [false, false, false, true];

    List<int> yellow1 = [0, 67, 100, 82];
    List<int> yellow2 = [20, 33, 0, 50];
    List<int> yellow3 = [0, 0, 0, 0];
    List<int> yellow4 = [100, 100, 100, 0];
    List<int> yellow5 = [50, 50, 50, 0];

    List<int> light1 = [50, 50, 50, 50];
    List<int> light2 = [50, 50, 50, 50];
    List<int> light3 = [100, 75, 50, 50];
    List<int> light4 = [100, 75, 50, 50];
    List<int> light5 = [100, 75, 50, 50];

    List<int> modeType1 = [0, 0, 0, 0];
    List<int> modeType2 = [0, 0, 0, 0];
    List<int> modeType3 = [0, 0, 0, 1];
    List<int> modeType4 = [0, 0, 0, 2];
    List<int> modeType5 = [0, 0, 0, 3];

    List<List<String>> names = [name1, name2, name3, name4, name5];
    List<List<bool>> pics = [isPic1, isPic2, isPic3, isPic4, isPic5];
    List<List<int>> yellows = [yellow1, yellow2, yellow3, yellow4, yellow5];
    List<List<int>> lights = [light1, light2, light3, light4, light5];
    List<List<int>> types = [modeType1, modeType2, modeType3, modeType4, modeType5];

    data = List.generate(names.length, (x) {
      return List.generate(
          names[x].length,
          (y) => Model(
              name: names[x][y],
              isPic: pics[x][y],
              yellow: yellows[x][y],
              light: lights[x][y],
              modeType: types[x][y])).toList();
    }).toList();
    refreshLocale();
  }

  ///刷新多语言
  void refreshLocale() {
    String sunny = loc?.sunny ?? '晴天';
    String rain = loc?.rain ?? '雨天';
    String fog = loc?.fog ?? '雾天';
    String snow = loc?.snow ?? '雪天';
    String city = loc?.city ?? '市区';
    String national_highway = loc?.national_highway ?? '国道';
    String high_speed = loc?.high_speed ?? '高速';
    String mountain_road = loc?.mountain_road ?? '山路';
    String lighting = loc?.lighting ?? '直射';
    String flashing = loc?.flashing ?? '闪烁';
    List<String> desList1 = [sunny, rain, fog, snow];
    List<String> desList2 = [city, national_highway, high_speed, mountain_road];
    List<String> desList3 = [lighting, lighting, lighting, flashing];
    List<String> desList4 = [lighting, lighting, lighting, flashing];
    List<String> desList5 = [lighting, lighting, lighting, flashing];
    List<List<String>> dess = [desList1, desList2, desList3, desList4, desList5];
    for (var i = 0; i < dess.length; i++) {
      List<Model> list = data[i];
      for (var j = 0; j < dess[i].length; j++) {
        Model m = list[j];
        m.modeDes = dess[i][j];
      }
    }
    if (weather.value.isNotEmpty) {
      weather.value = selectModel.value.modeDes;
    } else if (road.value.isNotEmpty) {
      road.value = selectModel.value.modeDes;
    } else if (mode.value.isNotEmpty) {
      mode.value = selectModel.value.modeDes;
    }
  }

  void clickTitle(BuildContext context, String title) {
    NavigationService.navigateToPage(context, title);
  }

  void changeValue(int row, int index) {
    var light = selectModel.value.light;
    selectModel.value = data[row][index];
    weather.value = '';
    road.value = '';
    mode.value = '';
    switch (row) {
      case 0: //天气
        weather.value = selectModel.value.modeDes;
        selectModel.value.light = light;
        break;
      case 1: //道路
        road.value = selectModel.value.modeDes;
        selectModel.value.light = light;
        break;
      case 2: //其他选择
      case 3:
      case 4:
        mode.value = selectModel.value.modeDes;
        selectModel.value.light = index == 3 ? light : data![row][index].light;
        break;
      default:
        break;
    }
    sendData(true);
  }

  void changeYellow(int value) {
    selectModel.value = Model(yellow: value, light: selectModel.value.light);
    sendData(false);
  }

  void changeLight(int value) {
    selectModel.value = Model(yellow: selectModel.value.yellow, light: value);
    sendData(false);
  }

  void sendData(bool ingore) {
    var light = max(5, selectModel.value.light);
    final now = DateTime.now();
    if (now.difference(_lastSendTime) >= _sendInterval || ingore) {
      _lastSendTime = now;
      SendData.send(
          tag: '颜色',
          data1: [selectModel.value.yellow, 100 - selectModel.value.yellow, light, selectModel.value.modeType]);
    } else {
      // 不满足时间间隔要求，不发送
    }
  }
}
